<?php
class Rdf extends Module {
    public function act_show() {
        $url = trim(tos_http_get('url'), '/');
        $params = explode('/', $url);
        
        $node_url_segment = '';
        if (isset($params[1])) {
            $node_url_segment = $params[1];
        } else {
            $node_url_segment = $params[0];
        }
        
        $node_url_segment = empty($node_url_segment) ? 'home' : $node_url_segment;
        
        $out = out();
        $block_display_node = m('node')->block_display_content_by_url_segment($node_url_segment);
        $out->add_block($block_display_node);
    }
    
    public function block_menu_product_galleries($selected_id = 0) {
        $galleries = $this->model('product')->get_product_galleries();
        return $this->view('node/menu', array('menu_items' => $galleries, 'selected_node_id' => $selected_id, 'parent_url_segment' => 'product-gallery'));
    }
    
    
    public function block_menu_product_ranges($selected_id = 0) {
        $ranges = $this->model('product')->get_product_ranges();
        return $this->view('node/menu', 
            array(
                'menu_items' => $ranges, 
                'selected_node_id' => $selected_id,
                'parent_url_segment' => 'product-range'));
    }
    
    
    
    public function block_tc_product_ranges() {
        $ranges = $this->model('product')->get_product_ranges(0, 2);
        return $this->view('node/tc_nodes', array('nodes' => $ranges));
    }
    
    
    public function block_products_by_gallery_id($gallery_id, $url_segment = '') {
        
        
        $page_index = (int) tos_http_get('page');
        $page_index = ($page_index <= 0) ? 1 : $page_index;
        $page_length = 8;
        $offset = ($page_index - 1) * $page_length;
        
        //$child_nodes = $this->model('node')->get_nodes_by_parent_id($parent_id, $offset, $page_length);
        $products = $this->model('product')->get_products_by_gallery_id($gallery_id,$offset, $page_length);
        
        $block = $this->view('rdf/products', array('products' => $products));
        $block->add_block(m('html')->block_pagination(array(
            'page_index' => $page_index,
            'page_count' => ceil($this->model('product')->get_count_by_gallery_id($gallery_id) / $page_length)
        )), 'pagination');
        
        
        return $block;
    }
    
    public function block_products_by_range_id($range_id) {
       $products = $this->model('product')->get_products_by_range_id($range_id);
       $model_node = m('node')->model('node');
       /*foreach ($products as &$product) {
           $product['url']= $model_node->get_url($product['id']);
       }*/
       return $this->view('rdf/range_products', array('products' => $products));
    }
    
    
    public function block_product_gallery_list() {
        $galleries = $this->model('product')->get_product_galleries();
        return $this->view('rdf/product_gallery_list', array('product_gallery_list' => $galleries));
    }
    
    
}
?>